//
//  ViewController.m
//  NTRoulette
//
//  Created by 孙勇 on 2020/3/28.
//  Copyright © 2020 yy. All rights reserved.
//

#import "ViewController.h"
#import "RouletteView.h"


@interface ViewController ()<CAAnimationDelegate>

@property (strong, nonatomic) RouletteView *roulette;
@property (copy,   nonatomic) NSString *prize;
@property (nonatomic) BOOL isAnimation;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.roulette = [[RouletteView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width - 50, self.view.bounds.size.width - 50)];
    self.roulette.center = self.view.center;
    [self.roulette.playButton addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.roulette];
}

- (void)clickBtn:(UIButton *)sender {
    if (self.isAnimation) {
        return;
    }
    self.isAnimation = YES;
    NSUInteger turnAngle;
    NSUInteger randomNum = arc4random() % 100; // 控制概率
//    NSUInteger turnsNum = arc4random() % 5 + 1; // 控制圈数
    NSUInteger turnsNum = 6;
    if (randomNum>=0 && randomNum<20) {//80%的概率 就是0-80
        
        if (randomNum < 40) {
            turnAngle = 0;
        }else{
            turnAngle = 135;
        }
        self.prize = self.roulette.numberArray[0];
        NSLog(@"抽中了500");
        
    } else if (randomNum>=20 && randomNum<40) {
        
        if (randomNum < 75) {
            turnAngle = 45;
        }else{
            turnAngle = 225;
        }
        self.prize = self.roulette.numberArray[3];
        NSLog(@"抽中了鲜花");
        
    } else if (randomNum >=40 && randomNum<60) {
        
        turnAngle = 315;
        self.prize = self.roulette.numberArray[1];
        NSLog(@"抽中了2000");
        
    } else if(randomNum >=60 && randomNum<80){
        
        if (randomNum < 95) {
            turnAngle = 90;
        }else{
            turnAngle = 270;
        }
        self.prize = self.roulette.numberArray[2];
        NSLog(@"抽中了5000");
        
    }else{
        
        turnAngle = 180;
        self.prize = self.roulette.numberArray[4];
        NSLog(@"抽中了20000");
    }
    CGFloat perAngle = M_PI/180.0;
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:turnAngle * perAngle + 360 * perAngle * turnsNum];
    rotationAnimation.duration = 3.0;
    rotationAnimation.cumulative = YES;
    rotationAnimation.delegate = self;
    
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.removedOnCompletion = NO;
    
    [self.roulette.rotateWheel.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"恭喜您抽中%@",self.prize] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.isAnimation = NO;
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

@end
